﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;

using CanDoo.Core;
using CanDoo;
using CanDoo.Data;
using CanDoo.Web;

using CanDoo.Admin.Core.Models;
using CanDoo.Admin.Core.Contracts;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using FineUIPro;
using CanDoo.FineUI;
using CanDoo.Extensions;


namespace CanDoo.Oil.WebApp.sysAdministrator
{
    public partial class Default : CanDoo.FineUI.ListPageBase<IAdministratorContract, Administrator>
    {
        public Default()
        {
            this.Selector = s => new
            {
                s.Id,
                s.Name,
                s.Sex,
                s.User,
                s.Email,
                s.Address,
                s.Department,
                s.DepartmentId,
                s.Mobile,
                s.Phone,
                s.QQ,
                s.Birthday,
                s.IDCard,
                s.PageSize,
                s.Remark,
                s.CreatedTime
            };
        }

        #region PagePower


        /// <summary>
        /// 本页面的功能模块
        /// </summary>
        public override string PageTitle
        {
            get
            {
                return "系统管理员";
            }
        }

        #endregion

        public IRoleContract RoleContract { get; set; }
        public IDepartmentContract DepartmentContract { get; set; }

        public override string IncludeProperties
        {
            get
            {
                if (string.IsNullOrEmpty(base.IncludeProperties))
                    return "Roles";
                return base.IncludeProperties;  
            }

            set
            {
                base.IncludeProperties = value;
            }
        }

        #region 方法
        protected override void Search()
        {
            var Key = SearchKey.GetValue();
            var roleId = SearchRole.GetValue().ToLong();
            if (!Key.IsEmpty())
                Filter = Filter.And(p => p.User.UserName.Contains(Key) || p.Name.Contains(Key));
            if (roleId != 0L)
                Filter = Filter.And(u => u.User.Roles.Any(r => r.Id == roleId));
        }

        //TODO : 需要显示管理员所属权限

        protected string ShowRoles(object value)
        {
            if(value==null)
                return "";
            else
            {
                var roles = (IEnumerable<Role>)value;
                return string.Join(",", roles.Select(p=>p.Name).ToArray());
            }
        }
        #endregion

        #region 事件
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FineUIListHelper.BindDDL(RoleContract.EntityName, this.RoleContract.GetSelectList(), 0L, SearchRole);
                InitGrid();
            }
        }
        #endregion

        #endregion 事件

    }
}
